package com.flyduck.system.controller;

import com.flyduck.common.annotation.IgnoreAuth;
import com.flyduck.common.vo.PageResult;
import com.flyduck.common.vo.Result;
import com.flyduck.system.service.SysDictTypeService;
import com.flyduck.system.vo.dictData.DictOptionVO;
import com.flyduck.system.vo.dictType.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: flyduck-platform
 * @description:
 * @author: flyduck
 * @create: 2021-03-23 20:37
 **/
@Api(tags = "系统:字典类型接口")
@RestController
@RequestMapping("/system/dict")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @ApiOperation("获取字典类型分页列表")
    @PostMapping("/page")
    public Result getDictTypePageList(@RequestBody SysDictTypeReqVO reqVo,
                                   @RequestParam(value = "page",required = true) Integer page,
                                   @RequestParam(value = "size",required = true) Integer size){
        PageResult<SysDictTypeRespVO> userPage = sysDictTypeService.getDictTypePageList(reqVo, page, size);
        return Result.success(userPage);
    }

    @IgnoreAuth
    @ApiOperation("获取字典类型详情")
    @GetMapping("/detail")
    public Result getDictTypeDetail(@RequestParam(value = "id",required = true) Long id){
        SysDictTypeDetailRespVO respVO = sysDictTypeService.getDictTypeDetail(id);
        return Result.success(respVO);
    }

    @ApiOperation("新增字典类型")
    @PostMapping("/add")
    public Result addDictType(@RequestBody @Validated SysDictTypeAddReqVO reqVo){
        boolean flag = sysDictTypeService.addDictType(reqVo);
        return Result.success(flag,"新增成功");
    }

    @ApiOperation("编辑字典类型")
    @PutMapping("/update")
    public Result updateDictType(@RequestBody @Validated SysDictTypeUpdateReqVO reqVo){
        boolean flag = sysDictTypeService.updateDictType(reqVo);
        return Result.success(flag,"编辑成功");
    }

    @ApiOperation("删除字典类型")
    @DeleteMapping("/delete")
    public Result deleteDictType(@RequestParam(value = "id",required = true) Long id){
        boolean flag = sysDictTypeService.deleteDictType(id);
        return Result.success(flag,"删除成功");
    }

    @IgnoreAuth
    @ApiOperation("获取字典选项")
    @GetMapping("/dict/options")
    public Result getDictOptionList(@RequestParam(value = "dictKey",required = true) String dictKey){
        List<DictOptionVO> dictOptionVOList = sysDictTypeService.getDictOptionList(dictKey);
        return Result.success(dictOptionVOList);
    }
}
